﻿using System;
using System.Data;
using System.Windows.Forms;

namespace MDMUI.Carrier
{
    public partial class DropCarriers : Form
    {
        private int selectedCount = 0;
        public DropCarriers()
        {
            InitializeComponent();
        }

        private void selectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable carrierData = DropCarrierBll.GetData();

                if (carrierData == null || carrierData.Rows.Count == 0)
                {
                    MessageBox.Show("没有可显示的载具数据。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // 显示查询到的数据条数
                SUNtxb.Text = carrierData.Rows.Count.ToString();

                // 添加选中列
                DataColumn selectedColumn = new DataColumn("Selected", typeof(bool));
                carrierData.Columns.Add(selectedColumn);

                // 将选中列移动到指定位置，例如移动到第一列
                selectedColumn.SetOrdinal(0);

                CLdataGridView.DataSource = carrierData;
                SetDataGridHeaders();

                // 设置选中列的默认值为 false
                foreach (DataRow row in carrierData.Rows)
                {
                    row["Selected"] = false;
                }

                // 绑定 DataGridView 的 CellValueChanged 事件
                CLdataGridView.CellValueChanged += CLdataGridView_CellValueChanged;

                // 绑定 DataGridView 的 CurrentCellDirtyStateChanged 事件
                CLdataGridView.CurrentCellDirtyStateChanged += CLdataGridView_CurrentCellDirtyStateChanged;

                MessageBox.Show($"已成功显示 {carrierData.Rows.Count} 条载具数据。", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"显示数据失败：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void CLdataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateSelectedCount();
        }

        private void CLdataGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (CLdataGridView.IsCurrentCellDirty)
            {
                CLdataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void UpdateSelectedCount()
        {
            selectedCount = 0;
            foreach (DataGridViewRow row in CLdataGridView.Rows)
            {
                if (row.Cells["Selected"].Value != null && (bool)row.Cells["Selected"].Value)
                {
                    selectedCount++;
                }
            }
            SENtxb.Text = selectedCount.ToString();
        }

        private void SetDataGridHeaders()
        {
            if (CLdataGridView.Columns.Count == 0) return;

            CLdataGridView.Columns["Selected"].HeaderText = "选中";
            CLdataGridView.Columns["Selected"].Width = 50;

            CLdataGridView.Columns["CarrierId"].HeaderText = "载具号";
            CLdataGridView.Columns["DurableSpecId"].HeaderText = "耐用品规格号";
            CLdataGridView.Columns["BatchId"].HeaderText = "批次号";
            CLdataGridView.Columns["CarrierType"].HeaderText = "载具类型";
            CLdataGridView.Columns["DetailedCarrierType"].HeaderText = "载具详细类型";
            CLdataGridView.Columns["CarrierStatus"].HeaderText = "载具状态";
            CLdataGridView.Columns["CleaningStatus"].HeaderText = "清洗状态";
            CLdataGridView.Columns["LockStatus"].HeaderText = "锁定状态";
            CLdataGridView.Columns["CapacityStatus"].HeaderText = "容量状态";
            CLdataGridView.Columns["Quantity"].HeaderText = "批次数量";
            CLdataGridView.Columns["LocationNumber"].HeaderText = "位置号";
            CLdataGridView.Columns["CarrierCapacity"].HeaderText = "载具容量";
        }
    }
}